#include<iostream>
#include"student.h"
using namespace std;



const int N = 10;

void menu();
void InputStu(const Student* array);
int InputStu(Student* array);
void InputSel(Subject* Selected, int);
int SearchStu(const Student* array, string na);   // 你没有这个函数的定义，只有声明，所以链接出现了问题
int InsertStu(Student* array);
bool DeleteStu(Student* array, Subject* Selected, string na);


void menu()
{
    cout << "******1.录入信息******" << endl;
    cout << "******2.查询信息******" << endl;
    cout << "******3.浏览信息******" << endl;
    cout << "******4.插入信息******" << endl;
    cout << "******5.删除信息******" << endl;
    cout << "******0.退    出******" << endl;
}

void OutputStu(const Student* array)
{
    cout << "学生总人数 = " << Student::GetCount() << endl;
    for (int i = 0; i < N; i++)
        if (array[i].GPA != -1)
        {
            cout << "姓 名：" << array[i].name << endl;
            cout << "学 号：" << array[i].ID << endl;
        }
}

int SearchStu(const Student* array, string na)
{
    int i, j = N;
    for (i = 0; i <  N; i++)
        if (array[i].GetGPA() != -1)
            if (array[i].GetName() == na)
            {
                j = i;
                break;
            }
    return j;
}

int InputStu(Student* array)
{
    int i = 0;
    if (Student::GetCount() == N)
        cout << "人数已满，无法继续录入！" << endl;
    else
    {
        while (array[i].GetGPA() > 0)
            i++;
        array[i].Input();
    } 
    return i;
}

void InputSel(Subject* Selected, int i)
{
    Selected[i].Input();
}

int InsertStu(Student* array)
{
    int i = 0;
    if (Student::GetCount() == N)
    {
        cout << "人数已满，无法插入记录！" << endl;
        return 0;
    }
    while (array[i].GetGPA() != -1)
        i++;
    array[i].Insert();
    return i;
}

bool DeleteStu(Student* array, Subject* Selected, string na)
{
    if (Student::GetCount() == 0)
    {
        cout << "没有记录，无法删除！" << endl;
        return false;
    }
    int i = SearchStu(array, na);
    if (i == N)
    {
        cout << "查无此人，无法删除！" << endl;
        return false;
    }
    array[i].Delete();
    Selected[i].Delete();
    return true;
}

int main()
{
    Student array[N];
    Subject Selected[N];
    int choice, i;
    string na;
    char ch = 'Y';
    do
    {
        menu();
        cout << "Please input your choice:";
        cin >> choice;
        if (choice >= 0 && choice <= 5){
            switch(choice)
            {
            case 1:
                while(ch == 'Y' || ch == 'y')
                {
                    i = InputStu(array);
                    InputSel(Selected, i);
                    array[i].ReckonGPA(Selected[i]);
                    cout << "继续输入吗？请输入 Y or N：";
                    cin >> ch;
                }
                break;
            case 2:
                cout << "Input the same searched:" << endl;
                cin >> na;
                int i;
                i = SearchStu(array, na);
                if (i == N)
                    cout << "查无此人！" << endl;
                else
                    array[i].Display(Selected[i]);
                break;
            case 3:
                OutputStu(array);
                break;
            case 4:
                i = InsertStu(array);
                if (i)
                {
                    cout << "成功插入一条记录\n";
                    cout << "录入成绩吗？请输入 Y or N:";
                    cin >> ch;
                    if (ch == 'y' || ch == 'Y')
                    {
                        Selected[i].Input();
                        array[i].ReckonGPA(Selected[i]);
                    }
                }
                else{
                    cout << "插入失败！" << endl;
                }
            case 5:
                cout << "Input the name deleted:" << endl;
                cin >> na;
                if (DeleteStu(array, Selected, na))
                {
                    cout << "成功删除一条记录" << endl;
                }
                else
                {
                    cout << "删除失败！" << endl;
                }
                break;
            default:
                break;
            }
        }
    } while (choice);
    
    return 0;
}

// void menu()
// {
//     cout << "******1.录入信息******" << endl;
//     cout << "******2.查询信息******" << endl;
//     cout << "******3.浏览信息******" << endl;
//     cout << "******4.插入信息******" << endl;
//     cout << "******5.删除信息******" << endl;
//     cout << "******0.退    出******" << endl;
// }

// void OutputStu(const Student* array)
// {
//     cout << "学生总人数 = " << Student::GetCount() << endl;
//     for (int i = 0; i < N; i++)
//         if (array[i].GPA != -1)
//         {
//             cout << "姓 名：" << array[i].name << endl;
//             cout << "学 号：" << array[i].ID << endl;
//         }
// }

// int InputStu(Student* array)
// {
//     int i = 0;
//     if (Student::GetCount() == N)
//         cout << "人数已满，无法继续录入！" << endl;
//     else
//     {
//         while (array[i].GetGPA() > 0)
//             i++;
//         array[i].Input();
//     } 
//     return i;
// }

// void InputSel(Subject* Selected, int i)
// {
//     Selected[i].Input();
// }

// int InsertStu(Student* array)
// {
//     int i = 0;
//     if (Student::GetCount() == N)
//     {
//         cout << "人数已满，无法插入记录！" << endl;
//         return 0;
//     }
//     while (array[i].GetGPA() != -1)
//         i++;
//     array[i].Insert();
//     return i;
// }

// bool DeleteStu(Student* array, Subject* Selected, string na)
// {
//     if (Student::GetCount() == 0)
//     {
//         cout << "没有记录，无法删除！" << endl;
//         return false;
//     }
//     int i = SearchStu(array, na);
//     if (i == N)
//     {
//         cout << "查无此人，无法删除！" << endl;
//         return false;
//     }
//     array[i].Delete();
//     Selected[i].Delete();
//     return true;
// }

